package cn.hhchat.wp.controller;

import cn.hhchat.wp.dto.form.CommentForm;
import cn.hhchat.wp.dto.msg.RMsg;
import cn.hhchat.wp.dto.msg.RMsgFactory;
import cn.hhchat.wp.entity.CommentEntity;
import cn.hhchat.wp.entity.PostEntity;
import cn.hhchat.wp.entity.UserEntity;
import cn.hhchat.wp.service.CommentService;
import cn.hhchat.wp.service.PostService;
import cn.hhchat.wp.util.SessionUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * Created this one by HMH on 2017/10/22.
 */
@RequestMapping("/comment")
@RestController
public class CommentController {

    @PutMapping
    public RMsg create(@RequestBody CommentForm form){
        UserEntity user = SessionUtil.getUser();
        PostEntity post = postService.getById(form.getPostId());
        CommentEntity comment = commentService.createOne(user, post, form.getContent());
        return RMsgFactory.ok(comment.toSimpleComment());
    }

    @Autowired
    private PostService postService;

    @Autowired
    private CommentService commentService;

}
